﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace loveMotorcade.web.Classes
{
    public class paging
    {
        /// <summary>
        /// 每页记录数
        /// </summary>
        public int pageSize = 20;

        /// <summary>
        /// 上一页的文本
        /// </summary>
        public string prevText = "< 上一页";

        /// <summary>
        /// 下一页的文本
        /// </summary>
        public string nextText = "下一页 >";

        /// <summary>
        /// 省略号文本
        /// </summary>
        public string ellipsisText = "...";

        /// <summary>
        /// 记录总数
        /// </summary>
        public int itemCount
        {
            get;
            private set;
        }

        /// <summary>
        /// 最大页码 起始为1
        /// </summary>
        int pageMax
        {
            get
            {
                return (itemCount - 1) / pageSize + 1;
            }
        }

        public IEnumerable<T> content<T>(IEnumerable<T> data, out dynamic pageInfo, int pageNum = 1)
            where T : class
        //where T : System.Data.Objects.DataClasses.EntityObject
        {
            if (data == null || !data.Any())
                this.itemCount = 1;
            else
                this.itemCount = data.Count();

            pageInfo = new List<pageNumStyle>();

            //使pageNum合法
            pageNum = pageNum < 1 ? 1 : pageNum;
            pageNum = pageNum > pageMax ? pageMax : pageNum;

            //全部页码
            for (int i = 1; i <= pageMax; i++)
                pageInfo.Add(pageNumStyle.create(i, i.ToString(), pageNum != i, pageNum == i));

            pageInfo = reduce(pageInfo);
            pageInfo = added(pageInfo);

            return data.Skip(getSkipNum(pageNum)).Take(pageSize);
        }

        /// <summary>
        /// 得到需要跳过的记录数
        /// </summary>
        /// <param name="pageNum"></param>
        /// <returns></returns>
        int getSkipNum(int pageNum)
        {
            return (pageNum - 1) * pageSize;
        }

        /// <summary>
        /// 补充 上一页 下一页 等
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        List<pageNumStyle> added(List<pageNumStyle> pagingList)
        {
            var pageNum = pagingList.Find(d => !d.isLink).pageNum;

            //补充省略号
            int i = 0;
            var listDot = new List<pageNumStyle>();
            foreach (var item in pagingList)
            {
                if (item.pageNum - i > 1)
                    listDot.Add(pageNumStyle.create(i + 1, ellipsisText, false));
                i = item.pageNum;
            }

            pagingList.AddRange(listDot);
            pagingList = pagingList.OrderBy(d => d.pageNum).ToList();

            List<pageNumStyle> list = new List<pageNumStyle>();

            list.Add(pageNumStyle.create(pageNum - 1, prevText, pageNum != 1));
            list.AddRange(pagingList);
            list.Add(pageNumStyle.create(pageNum + 1, nextText, pageNum != pageMax));

            return list;
        }

        /// <summary>
        /// 减少多余的页
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        List<pageNumStyle> reduce(List<pageNumStyle> pagingList)
        {
            var pageNum = pagingList.Find(d => !d.isLink).pageNum;

            var pageStart = pagingList.Take(3);
            var pageEnd = pagingList.Skip(pageMax - 3);
            var pageMiddle = pagingList.Where(d => d.pageNum < pageNum + 3 && d.pageNum > pageNum - 3);

            return pageStart.Union(pageEnd).Union(pageMiddle).OrderBy(d => d.pageNum).ToList();
        }

        /// <summary>
        /// 页面样式
        /// </summary>
        public class pageNumStyle
        {
            /// <summary>
            /// 页号
            /// </summary>
            public int pageNum;
            /// <summary>
            /// 连接文本
            /// </summary>
            public string linkText;
            /// <summary>
            /// 是否为链接
            /// </summary>
            public bool isLink;
            /// <summary>
            /// 是否是上一页，下一页等导航类
            /// </summary>
            public bool isSelected;

            public static pageNumStyle create(int pageNum, string linkText, bool isLink = true, bool isSelected = false)
            {
                var re = new pageNumStyle();
                re.pageNum = pageNum;
                re.linkText = linkText;
                re.isSelected = isSelected;
                re.isLink = isLink;
                return re;
            }
        }

    }
}